package com.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;

public class MyJSONUtil {

    public static String toJsonString(Object o,String timeFormat){
        ObjectMapper mapper = new ObjectMapper();
        //配置不使用时间戳
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        mapper.setDateFormat(sdf);
        try {
            return mapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            System.out.println("Json转换异常");
            e.printStackTrace();
        }
        return null;
    }
    public static String toJsonString(Object o){
        return toJsonString(o,"yyyy/MM/dd HH:mm:ss");
    }
}
